#include <mem.h>
#include <io.h>
#include <dos.h>
#include <alloc.h>

void own_setmem(void huge *dest, long length, char value)
{
unsigned int q,qlast,i;

if(length<32768L){
  setmem(dest, (unsigned int)length, value);
  return;
		 }
q=(unsigned int)(length/16384); qlast=(unsigned int)(length%16384);
for(i=0;i<q;i++)
  setmem((char huge *)dest+i*16384L, 16384, value);
if(qlast!=0)
  setmem((char huge *)dest+q*16384L, qlast, value);
return;
}


long own_read(int handle, void huge *buf, long len)
{
long count;
unsigned int q,qlast,i;

if(len<65000L){
  count=_read(handle, buf, (unsigned int)len);
  return count;
	      }
q=(unsigned int)(len/32768L); qlast=(unsigned int)(len%32768L);
count=0;
for(i=0;i<q;i++)
  count+=_read(handle, (char huge *)buf+i*32768L, 32768U);
if(qlast!=0)
  count+=_read(handle, (char huge *)buf+q*32768L, qlast);
return count;
}


long own_write(int handle, void huge *buf, long len)
{
long count;
unsigned int q,qlast,i;

if(len<65000L){
  count=_write(handle, buf, (unsigned int)len);
  return count;
	      }
q=(unsigned int)(len/32768L); qlast=(unsigned int)(len%32768L);
count=0;
for(i=0;i<q;i++)
  count+=_write(handle, (char huge *)buf+i*32768L, 32768U);
if(qlast!=0)
  count+=_write(handle, (char huge *)buf+q*32768L, qlast);
return count;
}

void own_movedata(void huge *srcptr,void huge *destptr,long n)
{
int q,qlast,i;
void huge *tptr1, huge *tptr2;

if(n<65000L){
  movedata(FP_SEG(srcptr),FP_OFF(srcptr),FP_SEG(destptr),FP_OFF(destptr),
           (unsigned int)n);
  return;
	    }
q=(unsigned int)(n/32768L); qlast=(unsigned int)(n%32768L);
for(i=0;i<q;i++){
  tptr1=(char huge *)srcptr+i*32768L;
  tptr2=(char huge *)destptr+i*32768L;
  movedata(FP_SEG(tptr1),FP_OFF(tptr1),FP_SEG(tptr2),FP_OFF(tptr2),32768U);
		}
if(qlast!=0){
  tptr1=(char huge *)srcptr+q*32768L;
  tptr2=(char huge *)destptr+q*32768L;
  movedata(FP_SEG(tptr1),FP_OFF(tptr1),FP_SEG(tptr2),FP_OFF(tptr2),qlast);
	    }
return;
}

unsigned long own_farcoreleft(void)
{
unsigned long space;

space=farcoreleft();
return space;
}

long own_getdfree(int drive)
{
struct dfree freespace;
long fsp;

getdfree(drive,&freespace);
fsp=(long)freespace.df_avail*freespace.df_bsec*freespace.df_sclus;
return fsp;
}

void far *own_farmalloc(unsigned long nbytes)
{
void far *ptr;

ptr=farmalloc(nbytes);
return ptr;
}

void own_farfree(void far *block)
{
farfree(block);
}